﻿package mortal.game.view.mining
{
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.sceneInfo.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.activeTime.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.copy.ChallengeCopy.view.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.task.data.*;
    import mortal.game.view.task.guild.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class MiningModule extends BaseWindow
    {
        private var _bmpBg:GImageBitmap;
        private var _stars:StarPointPanel;
        private var _bmpTimes:BitmapNumberText;
        private var _items:Array;
        private var _txtTaskNum:BitmapNumberText;
        private var _doubleCountDown:DoubleCountDownBox;
        private var _btnGet:GLoadingButton;
        private var _btnCYSB:GLoadingButton;
        private var _btnLJQW:GLoadingButton;
        private var _leftSeconds:int = 0;
        private var _lastcanGet:Boolean = false;
        private var _taskInfo:TaskInfo;
        private var _isDouble:Boolean = false;
        private var _cords:Array;
        private var _times:Array;
        public static var isDoubleRewards:Boolean = false;
        public static var isShow:Boolean = false;

        public function MiningModule(param1:ILayer = null)
        {
            this._cords = [130, 489, 71, 282, 328, 172, 596, 281, 515, 490];
            this._times = [1, 1.2, 1.4, 1.6, 2];
            super(param1);
            this.layer = LayerManager.windowLayer;
            this.setSize(764, 587);
            this.title = Language.getString(21044);
            return;
        }// end function

        public function get isDouble() : Boolean
        {
            return this._isDouble;
        }// end function

        public function set isDouble(param1:Boolean) : void
        {
            this._isDouble = param1;
            if (isDisposed)
            {
                return;
            }
            if (param1)
            {
                DisplayUtil.removeMe(this._btnGet);
                this.addChild(this._btnCYSB);
                this.addChild(this._btnLJQW);
            }
            else
            {
                DisplayUtil.removeMe(this._btnCYSB);
                DisplayUtil.removeMe(this._btnLJQW);
                this.addChild(this._btnGet);
            }
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            isShow = true;
            GuideAutoDoTask.isNotAutoDoTask = true;
            return;
        }// end function

        override public function stageResize() : void
        {
            this.x = (Global.stage.stageWidth - 764) / 2;
            this.y = (Global.stage.stageHeight - 587) / 2;
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            isShow = false;
            GuideAutoDoTask.isNotAutoDoTask = false;
            if (!AIManager.isWorking)
            {
                if (this._taskInfo != null && this._taskInfo.isDoing())
                {
                    TaskUtil.linkTask(this._taskInfo);
                }
            }
            return;
        }// end function

        public function updateAllInfos() : void
        {
            this._taskInfo = Cache.instance.mining.getMiningTask();
            var _loc_1:* = Cache.instance.mining.getTodayColor();
            this.updateTask(this._taskInfo, _loc_1);
            this.updateTodayTimes();
            if (Cache.instance.npc.selectedNpc == null)
            {
                EffectManager.glowFilterReg(this._btnGet, null, 0.2, 4, 0.1);
            }
            else
            {
                EffectManager.glowFilterUnReg(this._btnGet);
            }
            return;
        }// end function

        public function updateTodayTimes() : void
        {
            var _loc_1:* = Cache.instance.task.getGroupFinishCount(ETaskGroup._ETaskGroupMinning);
            if (_loc_1 >= 3)
            {
                this._btnGet.visible = true;
            }
            this._txtTaskNum.text = _loc_1 + "/3";
            this._bmpTimes.text = Cache.instance.mining.getTodayHelpOthersNum().toString() + "/" + GameConst.TRoutineTaskRefreshCount.toString();
            this._stars.value = Cache.instance.mining.getTodayStar();
            return;
        }// end function

        private function updateTask(param1:TaskInfo, param2:int) : void
        {
            var _loc_5:Array = null;
            var _loc_6:GuildTaskItem = null;
            var _loc_7:Number = NaN;
            var _loc_8:Point = null;
            if (isDisposed)
            {
                return;
            }
            var _loc_3:* = this._times[0];
            if (param1 == null || param1.isCanget())
            {
                _loc_3 = this._times[param2];
            }
            else
            {
                _loc_3 = this._times[param1.playerTask.extend];
            }
            if (param1 != null)
            {
                _loc_5 = this.getRewards(param1.stask.rewards);
            }
            else
            {
                _loc_5 = [198912 * _loc_3, 3200 * _loc_3];
            }
            var _loc_4:int = 0;
            while (_loc_4 < this._items.length)
            {
                
                _loc_6 = this._items[_loc_4];
                _loc_7 = this._times[_loc_4] / _loc_3;
                _loc_6.updateTask(_loc_4, _loc_5[1] * _loc_7, _loc_5[0] * _loc_7);
                if (this._times[_loc_4] == _loc_3)
                {
                    _loc_6.setSelected(true);
                    if (this._lastcanGet && this._taskInfo != null && this._taskInfo.isDoing())
                    {
                        _loc_8 = this._btnGet.localToGlobal(GameConst.yellowPoint);
                        _loc_8.x = _loc_8.x + 30;
                        _loc_8.y = _loc_8.y + 10;
                        _loc_6.flyToPoint(_loc_8);
                    }
                }
                else
                {
                    _loc_6.setSelected(false);
                }
                _loc_4++;
            }
            if (this._isDouble)
            {
                this.isDouble = true;
            }
            else
            {
                this.isDouble = false;
                this.addChild(this._btnGet);
                this.addChild(this._doubleCountDown);
                if (this._taskInfo == null)
                {
                    this._btnGet.styleName = ImagesConst.MingLQRWBtn_upSkin;
                }
                else if (this._taskInfo.isCanget())
                {
                    this._btnGet.styleName = ImagesConst.MingLQRWBtn_upSkin;
                }
                else if (this._taskInfo.isDoing())
                {
                    this._btnGet.styleName = ImagesConst.MingJXRWBtn_upSkin;
                }
                else if (this._taskInfo.isCanNotEnd() || this._taskInfo.isFail() || this._taskInfo.isComplete())
                {
                    this._btnGet.styleName = ImagesConst.MingWCRWBtn_upSkin;
                }
            }
            this._lastcanGet = this._taskInfo && this._taskInfo.isCanget();
            return;
        }// end function

        public function getRewards(param1:Array) : Array
        {
            var _loc_4:STaskReward = null;
            var _loc_5:SReward = null;
            var _loc_2:Array = [];
            var _loc_3:int = 0;
            while (_loc_3 < param1.length)
            {
                
                _loc_4 = STaskReward(param1[_loc_3]);
                _loc_5 = _loc_4.reward;
                switch(_loc_5.type)
                {
                    case EReward._ERewardExp:
                    {
                        _loc_2[0] = _loc_5.num;
                        break;
                    }
                    case EReward._ERewardMoney:
                    {
                        _loc_2[1] = _loc_5.num;
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                _loc_3++;
            }
            return _loc_2;
        }// end function

        override protected function updateBtnSize() : void
        {
            if (_closeBtn)
            {
                _closeBtn.x = 775 - _closeBtn.width - 20;
                _closeBtn.y = 10;
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            var _loc_5:GuildTaskItem = null;
            super.createDisposedChildrenImpl();
            this._bmpBg = UIFactory.gImageBitmap(null, 10, 32, this);
            this._bmpBg.imgUrl = ImagesConst.MiningWinBg;
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.MiningYSBg, 43, 76, this));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.Mining_JRYS, 115, 82, this));
            this._stars = UIFactory.getUICompoment(StarPointPanel);
            this._stars.x = 86;
            this._stars.y = 112;
            this.addChild(this._stars);
            this._stars.setStarStyle(ImagesConst.WiardStarLight, ImagesConst.WiardStarDark, 28);
            this._stars.totalStar = 5;
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.MiningYSBg, 495, 76, this));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.Mining_BGCS, 562, 82, this));
            this._bmpTimes = UIFactory.bitmapNumberText(584, 113, "MiningHelpTimeNum.png", 15, 22, -1, this, 14);
            this._items = [];
            var _loc_1:int = 0;
            while (_loc_1 < 5)
            {
                
                _loc_5 = UICompomentPool.getUICompoment(GuildTaskItem) as GuildTaskItem;
                _loc_5.pzPre = "MiningPZ";
                this.addChild(_loc_5);
                _loc_5.x = this._cords[_loc_1 * 2];
                _loc_5.y = this._cords[_loc_1 * 2 + 1] - 40;
                this._items.push(_loc_5);
                _loc_1++;
            }
            this._doubleCountDown = UICompomentPool.getUICompoment(DoubleCountDownBox);
            this._doubleCountDown.countDownUrl = ImagesConst.DoubleCountdownMing;
            this._doubleCountDown.startUrl = ImagesConst.doubleActiveRes_Mining;
            this._doubleCountDown.endUrl = ImagesConst.doubleActiveRes_MiningEnd;
            this._doubleCountDown.x = 240;
            this._doubleCountDown.y = 437;
            this.addChild(this._doubleCountDown);
            var _loc_2:* = ActiveTimeUtil.getSClientActive(EActiveType._EActiveTypeRoutine);
            this._doubleCountDown.sClientActive = _loc_2;
            var _loc_3:* = GlobalStyle.textFormatPutong;
            _loc_3.size = 18;
            _loc_3.color = 16574208;
            this._txtTaskNum = UIFactory.bitmapNumberText(382, 407, "MiningHelpTimeNum.png", 15, 22, -2, this, 14);
            _loc_3 = GlobalStyle.textFormatPutong;
            _loc_3.color = 15850817;
            _loc_3.align = TextFormatAlign.CENTER;
            _loc_3 = GlobalStyle.textFormatLv;
            _loc_3.size = 14;
            var _loc_4:* = UIFactory.gTextField("", 180, 32, 400, 24, this, _loc_3);
            _loc_4.text = Language.getString(21045);
            pushUIToDisposeVec(_loc_4);
            this._btnGet = UIFactory.gLoadingButton(ImagesConst.MingLQRWBtn_upSkin, 304, 510, 118, 44, this);
            this._btnCYSB = UIFactory.gLoadingButton(ImagesConst.MingSBCYBtn_upSkin, 226, 510, 118, 44);
            this._btnLJQW = UIFactory.gLoadingButton(ImagesConst.MingLJQWBtn_upSkin, 385, 510, 118, 44);
            this._btnGet.configEventListener(MouseEvent.CLICK, this.getTaskHandler);
            this._btnCYSB.configEventListener(MouseEvent.CLICK, this.gotoNpc);
            this._btnLJQW.configEventListener(MouseEvent.CLICK, this.flyToNpc);
            this._isDouble = false;
            return;
        }// end function

        private function continueTaskHandler(event:MouseEvent) : void
        {
            if (this._taskInfo == null)
            {
                return;
            }
            GameController.guide.guideAutoPathToNextStep(this._taskInfo);
            return;
        }// end function

        private function gotoNpc(param1) : void
        {
            if (this._taskInfo == null)
            {
                MsgManager.showRollTipsMsg(Language.getString(21046));
                return;
            }
            this._isDouble = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.MiningShowModule, false));
            TaskUtil.linkTask(this._taskInfo);
            return;
        }// end function

        private function flyToNpc(param1) : void
        {
            if (this._taskInfo == null)
            {
                MsgManager.showRollTipsMsg(Language.getString(21046));
                return;
            }
            this._isDouble = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.MiningShowModule, false));
            TaskUtil.linkTask(this._taskInfo, true);
            return;
        }// end function

        private function getTaskHandler(event:MouseEvent) : void
        {
            var _loc_4:SceneInfo = null;
            var _loc_5:NPCInfo = null;
            if (this._btnGet.styleName == "MingJXRWBtn")
            {
                this.continueTaskHandler(null);
                return;
            }
            var _loc_2:* = Cache.instance.task.getGroupFinishCount(ETaskGroup._ETaskGroupMinning);
            if (this._taskInfo == null && _loc_2 >= 3)
            {
                MsgManager.showRollTipsMsg(Language.getString(21046));
                return;
            }
            var _loc_3:* = ThingUtil.npcUtil.getNpc(GameConst.MiningFuncNpcId);
            if (_loc_3 == null)
            {
                _loc_4 = SceneConfig.instance.getSceneInfoByNpcId(GameConst.MiningFuncNpcId);
                if (_loc_4 == null)
                {
                    MsgManager.showRollTipsMsg(Language.getString(21047));
                    return;
                }
                _loc_5 = _loc_4.getNpcInfo(GameConst.MiningFuncNpcId);
                AIManager.cancelAll();
                if (AIManager.addMoveToOtherMap(Game.sceneInfo.sMapDefine.mapId, _loc_4.sMapDefine.mapId, new Point(_loc_5.snpc.point.x, _loc_5.snpc.point.y), GameConst.NpcInDistance, this.gotoAndGetTask, [1]))
                {
                    AIManager.startByCheckIsRuning();
                }
            }
            else
            {
                ThingUtil.selectEntity = _loc_3;
                Cache.instance.npc.selectedNpc = _loc_3;
                this.finishTaskHandler(null);
            }
            return;
        }// end function

        private function gotoAndGetTask(param1:int) : void
        {
            AIManager.addClickNpcAI(GameConst.MiningFuncNpcId);
            AIManager.addCallback(this.toClickGetTask);
            AIManager.startByCheckIsRuning();
            return;
        }// end function

        private function toClickGetTask() : void
        {
            this.finishTaskHandler(null);
            return;
        }// end function

        private function finishTaskHandler(event:MouseEvent) : void
        {
            var _loc_3:NPCInfo = null;
            if (this._taskInfo == null)
            {
                return;
            }
            var _loc_2:* = Cache.instance.npc.selectedNpc;
            if (_loc_2 != null)
            {
                _loc_3 = _loc_2.npcInfo;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAskNextStep, [this._taskInfo, _loc_3]));
            if (this._taskInfo.isComplete() || this._taskInfo.isFail())
            {
                if (Cache.instance.mining.getTodayDoTaskNum() >= GameConst.TMinningTaskMaxCount)
                {
                    GameController.mining.view.hide();
                    MsgManager.showRollTipsMsg(Language.getString(21046));
                }
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._isDouble = false;
            EffectManager.glowFilterUnReg(this._btnGet);
            this._bmpBg.dispose(param1);
            this._bmpBg = null;
            this._txtTaskNum.dispose(param1);
            this._txtTaskNum = null;
            if (this._doubleCountDown != null)
            {
                this._doubleCountDown.dispose(param1);
                this._doubleCountDown = null;
            }
            DisplayUtil.delNotUse(this._items, 0);
            this._items = null;
            if (this._btnGet != null)
            {
                this._btnGet.dispose(param1);
                this._btnGet = null;
            }
            if (this._btnLJQW != null)
            {
                this._btnLJQW.dispose(param1);
                this._btnLJQW = null;
            }
            if (this._btnCYSB != null)
            {
                this._btnCYSB.dispose(param1);
                this._btnCYSB = null;
            }
            this._stars.dispose(param1);
            this._stars = null;
            this._bmpTimes.dispose(param1);
            this._bmpTimes = null;
            return;
        }// end function

    }
}
